<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               v-model:search="search"
               v-model:page="page"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @on-load="onLoad">
      <template #menu-left>
        <el-button type="danger"
                   size="small"
                   icon="el-icon-delete"
                   plain
                   v-if="permission.usersalary_delete"
                   @click="handleDelete">删 除
        </el-button>
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/restaurant/usersalary";
  import { userListBySalary } from '@/api/restaurant/common';
  import {mapGetters} from "vuex";
  import { baseUrl } from '@/config/env';

  export default {
    data() {
      return {
        form: {},
        query: {},
        search: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          searchShow: true,
          searchMenuSpan: 6,
          tip: false,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          showSummary: true,
          sumColumnList: [
            { name: 'monthlySalary', type: 'sum', decimals: 2 },
            { name: 'dailySalary', type: 'sum' , decimals: 2 }
          ],
          column: [
            {
              label: "用户名",
              prop: "userId",
              multiple: false,
              filterable: true,
              type: "tree",
              dicUrl: baseUrl + "/restaurant/common/userListBySalary?disabled=true",
              rules: [{
                required: true,
                message: "请输入用户名",
                trigger: "click"
              }]
            },
            {
              label: "入职日期",
              prop: "hireDate",
              type: "date",
              format: 'YYYY-MM-DD',
              valueFormat: 'YYYY-MM-DD',
              rules: [{
                required: true,
                message: "请输入入职日期",
                trigger: "blur"
              }]
            },
            {
              label: "月薪",
              prop: "monthlySalary",
              type: 'number',
              precision: 2,
              min: 0,
              rules: [{
                required: true,
                message: "请输入月薪",
                trigger: "blur"
              }]
            },
            {
              label: "日薪",
              prop: "dailySalary",
              type: 'number',
              precision: 2,
              min: 0,
              disabled: true,
              rules: [{
                required: false,
                message: "请输入日薪",
                trigger: "blur"
              }]
            },
            {
              label: "余休天数",
              prop: "restDays",
              type: 'number',
              precision: 2,
              min: 0,
              rules: [{
                required: false,
                message: "请输入余休天数",
                trigger: "blur"
              }]
            },
            {
              label: "状态",
              prop: "status",
              type: "switch",
              search: true,
              props: {
                label: 'dictValue',
                value: 'dictKey'
              },
              dicUrl: baseUrl + "/blade-system/dict/dictionary?code=task_status",
              rules: [{
                required: true,
                message: "请输入状态",
                trigger: "blur"
              }]
            },
            {
              label: "订台",
              prop: "isBooker",
              type: "switch",
              dicData: [
                {
                  label: "否",
                  value: 0
                },
                {
                  label: "是",
                  value: 1
                }
              ],
            },
            {
              label: "看台",
              prop: "isStand",
              type: "switch",
              dicData: [
                {
                  label: "否",
                  value: 0
                },
                {
                  label: "是",
                  value: 1
                }
              ],
            },
            {
              label: "备注",
              prop: "remark",
              type: "textarea",
              rules: [{
                required: false,
                message: "请输入备注",
                trigger: "blur"
              }]
            },
          ]
        },
        data: [],
        userData: [],
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.validData(this.permission.usersalary_add, false),
          viewBtn: this.validData(this.permission.usersalary_view, false),
          delBtn: this.validData(this.permission.usersalary_delete, false),
          editBtn: this.validData(this.permission.usersalary_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {

        row.userName = this.userData.find(item => item.id === row.userId).realName;

        add(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        row.userName = this.userData.find(item => item.id === row.userId).realName;
        update(row).then(() => {
          done();
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;

          userListBySalary(true).then(res => {
            const column = this.findObject(this.option.column, "userId");
            column.dicData = res.data.data;
            this.userData = res.data.data;
          });

          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
